package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import model.Order;
import util.DBConnection;

public class OrderDAOImp implements OrderDAO {

	private static final String INSERT = "insert into d_order(id,user_id,status,order_time,order_desc,total_price,"
			+ "receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?,?,?,?,?,?)";
	private static final String FINDBYID = "select * from d_order where order_time=?";
	private static final String UPDATE = "update d_order set user_id=?,status=?,order_time=?,order_desc=?,total_price=?,"
			+ "receice_name=?,full_address=?,postal_code=?,mobiley=?,phone=? where id=?";

	@Override
	public void save(Order order) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		ResultSet rst = null;
		try {
			conn = DBConnection.getConnection();
			prep = conn.prepareStatement(INSERT, Statement.RETURN_GENERATED_KEYS);
			prep.setInt(1, order.getId());
			prep.setInt(2, order.getUserId());
			prep.setInt(3, order.getStatus());
			prep.setLong(4, order.getOrderTime());
			prep.setString(5, order.getOrderDesc());
			prep.setDouble(6, order.getTotalPrice());
			prep.setString(7, order.getReceiveName());
			prep.setString(8, order.getFullAddress());
			prep.setString(9, order.getPostalCode());
			prep.setString(10, order.getMoblie());
			prep.setString(11, order.getPhone());
			prep.executeUpdate();
			rst = prep.getGeneratedKeys();
			rst.next();

		} finally {
			DBConnection.close(rst, prep, conn);
		}
	}

	@Override
	public Order finById(long ordertime) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		ResultSet rst = null;
		Order order = null;
		try {
			conn = DBConnection.getConnection();
			prep = conn.prepareStatement(FINDBYID);
			prep.setLong(1, ordertime);
			rst = prep.executeQuery();
			if (rst.next()) {
				order = new Order();
				order.setId(rst.getInt("id"));
				order.setUserId(rst.getInt("user_id"));
				order.setReceiveName(rst.getString("receive_name"));
				order.setPostalCode(rst.getString("postal_code"));
				order.setMoblie(rst.getString("mobile"));
				order.setPhone(rst.getString("phone"));
				order.setFullAddress(rst.getString("full_address"));
				order.setStatus(rst.getInt("status"));
				order.setOrderTime(rst.getLong("order_time"));
				order.setOrderDesc(rst.getString("order_desc"));
				order.setTotalPrice(rst.getDouble("total_price"));
			}
		} finally {
			DBConnection.close(rst, prep, conn);
		}
		return order;
	}

	public void update(Order order) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		try {
			conn = DBConnection.getConnection();
			prep = conn.prepareStatement(UPDATE);
			prep.setInt(11, order.getId());
			prep.setInt(1, order.getUserId());
			prep.setInt(2, order.getStatus());
			prep.setLong(3, order.getOrderTime());
			prep.setString(4, order.getOrderDesc());
			prep.setDouble(5, order.getTotalPrice());
			prep.setString(6, order.getReceiveName());
			prep.setString(7, order.getFullAddress());
			prep.setString(8, order.getPostalCode());
			prep.setString(9, order.getMoblie());
			prep.setString(10, order.getPhone());
			prep.executeUpdate();

		} finally {
			DBConnection.close(prep, conn);
		}

	}
}
